layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var layer = layui.layer;

    /**
     * 公示表管理
     */
    var VoteInfo = {
        tableId: "VoteInfoTable"
    };

    /**
     * 初始化表格的列
     */
    VoteInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'isDel', sort: false, hide: true, title: '0正常 1删除'},
            {field: 'relXqId', sort: false, hide: true, title: '小区id'},
            {field: 'xqName', sort: false, title: '小区'},
            {field: 'wxXm', sort: false, title: '维修项目'},
            {
                field: 'tswxXm', sort: false, title: '特殊维修项目', templet: function (res) {
                    let specialVotetenanceProject = ""
                    if (res.tswxXm != null && res.tswxXm != '') {
                        let arr = res.tswxXm.split(",")
                        for (let i = 0; i < arr.length; i++) {
                            if (i < arr.length - 1) {
                                if (arr[i] == 1) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【电梯】,"
                                }
                                if (arr[i] == 2) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【消防】,"
                                }
                                if (arr[i] == 3) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【供水、供电、供气】,"
                                }
                            } else {
                                if (arr[i] == 1) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【电梯】"
                                }
                                if (arr[i] == 2) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【消防】"
                                }
                                if (arr[i] == 3) {
                                    specialVotetenanceProject = specialVotetenanceProject + "【供水、供电、供气】"
                                }
                            }
                        }
                    }
                    return specialVotetenanceProject;
                }
            },
            {field: 'sgfa', sort: false, title: '施工方案'},
            {
                field: 'gcys', sort: false, title: '工程预算', templet: function (res) {
                    return res.gcys + " 元"
                }
            },
            {
                field: 'ftl', sort: false, title: '分摊率', templet: function (res) {
                    let ftlNew = Number(res.ftl * 100).toFixed(1) + "%"
                    return ftlNew;
                }
            },
            {
                field: 'showDay', sort: false, title: '公示天数', templet: function (res) {
                    return res.showDay + " 天"
                }
            },
            {
                field: 'startDate', sort: false, title: '开始公示时间', templet: function (res) {
                    return res.startDate.substr(0, 10)
                }
            },
            {
                field: 'state', sort: false, title: '状态', templet: function (res) {
                    if (res.state == 2 || res.state == 1) {
                        return "发起公示"
                    }
                    if (res.state == 3) {
                        return "开始投票"
                    }
                    if (res.state == 4) {
                        return "公示投票结果"
                    }
                }
            },
            {field: 'createPerson', sort: false, hide: true, title: '创建人'},
            {field: 'createDate', sort: false, hide: true, title: '创建时间'},
            {field: 'updatePerson', sort: false, hide: true, title: '修改人'},
            {field: 'updateDate', sort: false, hide: true, title: '修改时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    $.ajax(Feng.ctxPath + '/xqInfo/listXqByUser', {
        success: function (res) {
            // console.log(res)
            $("#relXqId").empty();
            $("#relXqId").append("<option value=''>请选择小区</option>");
            for (let i = 0; i < res.length; i++) {
                $("#relXqId").append(new Option(res[i].xqName, res[i].id))
            }
            //重新渲染
            form.render("select");
        }
    })

    /**
     * 跳转到方案公示页面
     */
    $("#publicity").click(function () {
        if ($("#relXqId").val() == "") {
            Feng.error("请先选择要公示方案的小区")
            return;
        } else {
            window.location.href = Feng.ctxPath + '/voteInfo/add?relXqId=' + $("#relXqId").val()
        }
    })

    /**
     * 点击查询按钮
     */
    VoteInfo.search = function () {
        var queryData = {};

        queryData['createDate'] = $('#createDate').val();
        queryData['createPerson'] = $('#createPerson').val();
        queryData['ftl'] = $('#ftl').val();
        queryData['gcys'] = $('#gcys').val();
        queryData['id'] = $('#id').val();
        queryData['isDel'] = $('#isDel').val();
        queryData['relXqId'] = $('#relXqId').val();
        queryData['sgfa'] = $('#sgfa').val();
        queryData['showDay'] = $('#showDay').val();
        queryData['startDate'] = $('#startDate').val();
        queryData['state'] = $('#state').val();
        queryData['tswxXm'] = $('#tswxXm').val();
        queryData['updateDate'] = $('#updateDate').val();
        queryData['updatePerson'] = $('#updatePerson').val();
        queryData['wxXm'] = $('#wxXm').val();

        table.reload(VoteInfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    VoteInfo.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/voteInfo/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    VoteInfo.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/voteInfo/edit?id=' + data.id
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + VoteInfo.tableId,
        url: Feng.ctxPath + '/voteInfo/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: VoteInfo.initColumn(),
        skin: 'line',
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        VoteInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        VoteInfo.jumpAddPage();

    });


    VoteInfo.voteApprove = function (data) {
        // console.log(data)
        layer.open({
            title: '发起投票',
            type: 2,
            area: ['550px', '550px'],
            content: Feng.ctxPath + '/voteInfo/vote?main_id=' + data.id,
            tableId: VoteInfo.tableId,
            end: function () {
                table.reload(VoteInfo.tableId)
            }
        })
    }


    VoteInfo.showVoteInfo = function (data) {
        // console.log(data)
        layer.open({
            title: '投票详情',
            type: 2,
            area: ['650px', '550px'],
            content: Feng.ctxPath + '/voteInfo/showVoteInfo?main_id=' + data.id,
            tableId: VoteInfo.tableId,
            end: function () {
                table.reload(VoteInfo.tableId)
            }
        })
    }


    // 工具条点击事件
    table.on('tool(' + VoteInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Feng.error("暂时不可查看")
            return;
            VoteInfo.jumpEditPage(data);
        } else if (layEvent == 'voteApprove') {
            VoteInfo.voteApprove(data)
        } else if (layEvent == 'voteResult') {
            VoteInfo.showVoteInfo(data)
        }
    });
});
